package com.qf.controller;

import com.qf.Resp.BaseResp;
import com.qf.assetsappraisal.Assetsappraisal;
import com.qf.borrower.Borrower;
import com.qf.service.AssetsappraisalService;
import com.qf.utils.JWTUtils;
import com.qf.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
public class AssetsappraisalController {
    @Autowired
    AssetsappraisalService assetsappraisalService;
    @Autowired
    JWTUtils jwtUtils;

    /*@Autowired
    Upload upload;
    //查所有*/
    @RequestMapping("/findAllAssetsappraisal")
    public List<Assetsappraisal> findAllGoods() {
        return assetsappraisalService.findAllAssetsappraisal();
    }

    //申请表id查多个
    @RequestMapping("/findAllByBorrowerId")
//     public List<Assetsappraisal> findAllByBorrowerId(@RequestParam("borrowerId") Integer borrowerId) {//路径传参
    public List<Assetsappraisal> findAllByBorrowerId(@RequestBody Map map) {//josn传参
        Object borrowerId1 = map.get("borrowerId");
//        System.out.println("转为整型id：" + borrowerId1);
        String s = String.valueOf(borrowerId1);
//        System.out.println("转整型id：" + s);
        Integer borrowerId = Integer.valueOf(s);
        System.out.println("转为整型id：" + borrowerId);
       /* System.out.println("申请id：" + borrowerId1);
         Integer borrowerId = (Integer) borrowerId1;*/
//
//        System.out.println("转为整型id：" + borrowerId);
//        Assetsappraisal assetsappraisal = new Assetsappraisal();
//        assetsappraisal.setBorrowerId(borrowerId);
        /*   assetsappraisalService.findAllByBorrowerId(assetsappraisal);*/
        List<Assetsappraisal> list = assetsappraisalService.findAllByBorrowerId(borrowerId);
        return list;
    }

    @RequestMapping("/findByBorrowerId")
    public List<Assetsappraisal> findByBorrowerId() {//josn传参
        //从token中获取id
        Integer borrowerId = 1;

       /* System.out.println("申请id：" + borrowerId1);
         Integer borrowerId = (Integer) borrowerId1;*/
//
//        System.out.println("转为整型id：" + borrowerId);
//        Assetsappraisal assetsappraisal = new Assetsappraisal();
//        assetsappraisal.setBorrowerId(borrowerId);
        /*   assetsappraisalService.findAllByBorrowerId(assetsappraisal);*/
        List<Assetsappraisal> list = assetsappraisalService.findAllByBorrowerId(borrowerId);
        return list;
    }

/*    //申请表id和证书模糊查
    @RequestMapping("/findByBorrowerIdOrCertificateNameContaining")
        public List<Assetsappraisal> findByBorrowerIdOrCertificateNameContaining(@RequestBody Map map) {
            Integer borrowerId = (Integer) map.get("borrowerId");
            String certificateName = (String) map.get("certificateName");

       *//* if(borrowerId==null){
            return assetsappraisalService.findByCertificateNameLike(certificateName);//模糊查名称
        }*//*
        if (certificateName==null) {
           return assetsappraisalService.findAllByBorrowerId(assetsappraisal);
        }

//        System.out.println("1232432543543 ");
//        return assetsappraisalService.findByBorrowerIdOrCertificateNameContaining(borrowerId,certificateName);
        return Collections.emptyList();
    }*/

    //资产评估表id
    @RequestMapping("/findByAssetsappraisalId")
//    public Assetsappraisal findById(@RequestParam("Id") Integer id) {
    public Assetsappraisal findById(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        return assetsappraisalService.findById(id);
    }

    //增与改
    @RequestMapping("/saveOrUpdateAssetsappraisal")
    public String saveOrUpdateAssetsappraisal(@RequestBody Assetsappraisal assetsappraisal) {
        return assetsappraisalService.saveOrUpdate(assetsappraisal);
    }

    //id删
    @RequestMapping("/deleteByAssetsappraisalId")
//    public String deleteById(@RequestParam("Id") Integer id) {
    public String deleteById(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        return assetsappraisalService.deleteById(id);
    }


    //上传照片
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        Upload uploads = new Upload();
        String upload1 = uploads.upload(file);
        if (upload1 != "") {
            return upload1;
        } else {
            return "fail";
        }
    }

    //日期格式
    @RequestMapping("/date")
    public String datas(Map map) throws ParseException {
        String timeStr = (String) map.get("assessTime");
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
        Date dateTime = SDF.parse(timeStr);
        SimpleDateFormat Time3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String assessTime = Time3.format(dateTime);
        System.out.println("时间格式" + assessTime);
        return assessTime;
    }

    @RequestMapping("/saveOrUpdateByBorrowerId")
    public String saveOrUpdateByBorrowerId(@RequestBody Assetsappraisal assetsappraisal) {
        //HttpServletRequest req, @RequestBody Assetsappraisal assetsappraisal

    /*    //从tok中取借款人ID
        Cookie[] cookies = req.getCookies();
        String token = null;
        if (cookies != null) {
            token = getToken(cookies);
        } else {
            System.out.println("token为null");
        }


        Map verify = jwtUtils.Verify(token);
        Borrower o = (Borrower) verify.get("body");
        int borrowerid = o.getId();*/
        int borrowerid = 1;
        assetsappraisal.setBorrowerId(borrowerid);
        return assetsappraisalService.saveOrUpdate(assetsappraisal);
    }


    public String getToken(Cookie[] cookies) {
        String token = null;
        for (Cookie cook : cookies
        ) {
            if (cook.getName().equals("token")) {
                token = cook.getValue();
            }
        }
        return token;
    }

}
